﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AAEngine.Box2D;
using AAEngine.Engine;
using AAEngine.Engine.Entities;
using AAEngine.Rendering2D;
using AAEngine.Screens;
using AAEngine.Rendering2D.Renderers;

namespace Box2DTest.Screens
{
    public class GameplayScreen : GameScreen
    {
        SceneView2D _sceneView;
        float _pauseAlpha;
        private PlayerIndex? _controllingPlayer;

        public override void OnAdded()
        {
            base.OnAdding();

            AAGame.LevelManager.LoadLevel(1);

            _sceneView.TrackObject = AAGame.LookupComponentByType("Dude", typeof(RendererBase)) as RendererBase;
        }

        public override void LoadContent()
        {
            base.LoadContent();

            int width = ScreenManager.Game.GraphicsDevice.Viewport.Width;
            int height = ScreenManager.Game.GraphicsDevice.Viewport.Height;

            _sceneView = new SceneView2D();
            _sceneView.ViewArea = new Rectangle(0, 0, width, height);
            _sceneView.RenderQueue.SpriteBatch = new SpriteBatch(ScreenManager.GraphicsDevice);
            _sceneView.RenderQueue.PrimitiveBatch = new PrimitiveBatch(ScreenManager.GraphicsDevice);

            IEntity sceneEntity = AAGame.InitializeScene(_sceneView, "Scene", null, typeof(Box2DManagerComponent));
            sceneEntity.AddComponent(_sceneView, "SceneView");
        }

        public override void OnRemoved()
        {
            AAGame.LevelManager.UnloadCurrentLevel();

            base.OnRemoved();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            //_input.Update();
            PlayerIndex playerIndex;
            if (AAGame.InputManager.IsMenuCancel(_controllingPlayer, out playerIndex))
            {
//                LoadingScreen.Load(ScreenManager, false, _controllingPlayer, new BackgroundScreen("Images/background"), new MainMenuScreen(_controllingPlayer));
                return;
            }

            // Gradually fade in or out depending on whether we are covered by the pause screen.
            if (coveredByOtherScreen)
                _pauseAlpha = Math.Min(_pauseAlpha + 1f / 32, 1);
            else
                _pauseAlpha = Math.Max(_pauseAlpha - 1f / 32, 0);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            _sceneView.Render(ScreenManager.Game.GraphicsDevice);
        }
    }
}
